package com.zpark.dao.impl;

import com.zpark.dao.ISouHuDao;
import com.zpark.bean.Ranking;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class SouHuDaoImpl implements ISouHuDao {

    /**
     * jdbcTemplate.update适合于insert、update和delete操作。
     * jdbcTemplate.queryForObject用于查询单条记录返回结果。
     * jdbcTemplate.query用于查询结果列表
     * BeanPropertyRowMapper可以将数据库字段的值向对象映射，满足驼峰标识也可以自动映射。
     */
    // 自动注jdbc模板
    @Autowired
    private JdbcTemplate jdbc;

    @Override
    public List<Ranking> selectAll() {
        String sql = "select * from ranking";
        return jdbc.query(sql, new BeanPropertyRowMapper<>(Ranking.class));
    }

    @Override
    public Ranking selectById(Integer id) {
        String sql = "select * from ranking where id = ?";
        return jdbc.queryForObject(sql, new BeanPropertyRowMapper<>(Ranking.class), new Object[]{id});
    }

    @Override
    public Integer insert(Ranking ranking) {
        String sql = "insert into ranking(film_index, film_name, film_actor_name, film_feasibly) values(?, ?, ?, ?)";
        return jdbc.update(sql, ranking.getFilmIndex(), ranking.getFilmName(), ranking.getFilmActorName(), ranking.getFilmFeasibly());
    }

    @Override
    public Integer updateById(Integer id, String fName) {
        String sql = "update ranking set film_name = ?  where id = ?";
        return jdbc.update(sql, fName, id);
    }

    @Override
    public Integer deleteById(Integer id) {
        String sql = "delete from ranking where id = ?";
        return jdbc.update(sql, id);
    }
}
